package com.tfy.liquor.service.impl;

import com.tfy.liquor.bean.AdminUser;
import com.tfy.liquor.dao.AdminUserDao;
import com.tfy.liquor.service.AdminUserService;
import com.tfy.liquor.util.CodeUtils;
import com.tfy.liquor.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 管理员信息
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {
    @Autowired
    private AdminUserDao adminUserDao;

    // 添加管理员信息
    @Override
    public boolean addAdminUser(AdminUser adminUser) {
        // 加密明文密码
        String salt = CodeUtils.generateSalt();
        adminUser.setPassword(CodeUtils.md5Hex(adminUser.getPassword(), salt));
        // 获取当前时间
        adminUser.setCreateTime(DateUtil.getNowDateTime());
        // 添加数据
        return adminUserDao.addAdminUser(adminUser) > 0;
    }

    // 用户登录
    @Override
    public AdminUser getAdminUserInfo(AdminUser adminUser) {
        // 根据用户账号获取数据中的数据
        AdminUser adminUser1 = adminUserDao.getAdminUserInfo(adminUser.getUserName());
        // 对比提供的密码和数据库中的数据
        if (null != adminUser1 && adminUser1.getPassword().equals(CodeUtils.md5Hex(adminUser.getPassword(), adminUser1.getSalt()))) {
            return adminUser1;
        }
        return null;
    }

    // 添加用户时检查数据库中是否重复
    @Override
    public Boolean getAdminUser(String userName) {
        return null == adminUserDao.getAdminUser(userName);
    }


}
